package com.abigdreamer.im.client.resourse;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileSystemView;

import sun.awt.shell.ShellFolder;

/**
 * 
 */
public class ResourcesManagement {

	/**
	 * 
	 * @param imagePath
	 *            相对于ResourcesManagement的路径
	 * @return
	 */
	public static Image getImage(String imagePath, int x, int y) {
		java.net.URL url = ResourcesManagement.class.getResource(imagePath);
		Image image = Toolkit.getDefaultToolkit().getImage(url);
		image = image.getScaledInstance(x, y, Image.SCALE_SMOOTH);
		return image;
	}

	/**
	 * 
	 * @param imagePath
	 * @return
	 */
	public static Image getImage(String imagePath) {
		URL url = ResourcesManagement.class.getResource(imagePath);
		Image image = Toolkit.getDefaultToolkit().getImage(url);
		
		return image;
	}
	
	public static BufferedImage getBufferedImage(String imagePath) {
		URL url = ResourcesManagement.class.getResource(imagePath);
		try {
			return ImageIO.read(url);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static ImageIcon getImageIcon(String imagePath) {
		Image image = getImage(imagePath);
		return new ImageIcon(image);
	}
	
	/**
	 * 
	 * @param ext
	 * @return
	 */
	public static Icon getIconByExtractor(String ext, boolean isBig) {
		File file = null;
		Icon icon = null;
		try {
			file = File.createTempFile("icon", "." + ext);
			FileSystemView view = FileSystemView.getFileSystemView();
			if (isBig) {
				ShellFolder shellFolder = ShellFolder.getShellFolder(file);
				icon = new ImageIcon(shellFolder.getIcon(true));
			} else {
				icon = view.getSystemIcon(file);
			}
			file.delete();
		} catch (IOException e) {
			e.printStackTrace();
			icon = getIcon("ui/Explorer.png");
		}

		return icon;
	}

	/**
	 * 
	 * @param fileName
	 * @return
	 */
	public static String getFileExtractor(String fileName) {
		String[] strs = fileName.replace(".", ",").split(",");
		return strs[strs.length - 1];
	}

	/**
	 * 
	 * @param imagename
	 * @return
	 */
	public static Icon getIcon(String imagename) {
		java.net.URL url = ResourcesManagement.class.getResource(imagename);
		Icon icon = new ImageIcon(url);
		return icon;
	}

	/**
	 * 
	 * @param imagePath
	 * @return
	 */
	public static ImageIcon getImageIcon(String imagePath, int x, int y) {
		ImageIcon imageIcon = getImageIcon(imagePath);
		int width = imageIcon.getIconWidth();
		int height = imageIcon.getIconHeight();
		float xbili = (float) ((x * 1.0) / width);
		float ybili = (float) ((y * 1.0) / height);
		float bili = xbili > ybili ? ybili : xbili;
		int newwidth = (int) (width * bili);
		int newheight = (int) (height * bili);
		Image image = getImage(imagePath, newwidth, newheight);
		imageIcon.setImage(image);
		return imageIcon;
	}

}
